package com.bonc.ioc.controller;

import com.bonc.ioc.base.controller.BaseController;
import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.exceptions.McpException;
import com.bonc.ioc.service.StandardBasicDataOptionService;
import com.bonc.ioc.service.StandardBasicDataService;
import com.bonc.ioc.service.StandardDetailService;
import com.bonc.ioc.service.StandardService;
import com.bonc.ioc.util.BackUpDataBaseManager;
import com.bonc.ioc.util.ExcelUtils;
import com.bonc.ioc.validation.AppAuthAnnotation;
import com.bonc.ioc.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 内容展示相关接口 知识产权
 *
 * @author xsy
 * @date 2021/3/7 13:27
 */
@RestController
@RequestMapping("/standard")
@Slf4j
public class StandardController extends BaseController {

    @Autowired
    private StandardService standardService;

    @Autowired
    private StandardBasicDataService standardBasicDataService;
    @Autowired
    private StandardBasicDataOptionService standardBasicDataOptionService;

    @Autowired
    private StandardDetailService standardDetailService;

    @Autowired
    private BackUpDataBaseManager back;

    /**
     * saveAddictsInfo 保存标准信息
     *
     * @param item:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:41
     * @change 2021/3/18 15:41 by q8541q@163.com for init
     * @since 1.0.0
     */
    @PostMapping("/save")
    //    @AppAuthAnnotation
    public AppReply saveAddictsInfo(@RequestBody StandardVo item) {
        return AppReply.success(standardService.save(item));
    }

    /**
     * deleteMadInfo 删除标准信息
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:41
     * @change 2021/3/18 15:41 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/delete")
    @AppAuthAnnotation
    public AppReply deleteMadInfo(@RequestParam String id) {
        return AppReply.success(standardService.deleteByPrimaryKey(id));
    }

    /**
     * getStandardInfo 获取标准详情
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:44
     * @change 2021/3/18 15:44 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getStandardInfo")
    public AppReply getStandardInfo(@RequestParam String id) {
        return AppReply.success(standardService.selectByPrimaryKey(Long.valueOf(id)));
    }

    /**
     * getStandardInfoByPage 分页获取标准描述 分页信息
     *
     * @param
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/25 10:35
     * @change 2021/3/25 10:35 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getStandardInfoByPage/{choosedSjbs}")
    public AppReply getStandardInfoByPage(@PathVariable String choosedSjbs) throws BindException {
        PageData pageData = getPageData();
        pageData.put("sjbs", choosedSjbs);
        PageResult pageResult = standardService.selectStandardInfoByPage(pageData);

        return AppReply.success(pageResult);
    }

    /**
     * getBasicFieldInfoList 列表展示 标准元数据
     *
     * @param
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 14:13
     * @change 2021/3/26 14:13 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getBasicFieldInfoList")
    public AppReply getBasicFieldInfoList(@RequestParam String id) throws BindException {
        List<StandardBasicDataVo> list = standardBasicDataService.getBasicFieldInfoList(id);
        return AppReply.success(list);
    }

    /**
     * saveAddictsInfo 保存标准元信息
     *
     * @param item:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/18 15:41
     * @change 2021/3/18 15:41 by q8541q@163.com for init
     * @since 1.0.0
     */
    @PostMapping("/saveBasicFieldInfo")
    @AppAuthAnnotation
    public AppReply saveBasicFieldInfo(@RequestBody StandardBasicDataVo item) {
        return AppReply.success(standardBasicDataService.save(item));
    }

    /**
     * deleteBasicFieldInfo
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 15:00
     * @change 2021/3/26 15:00 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/deleteBasicFieldInfo")
    public AppReply deleteBasicFieldInfo(@RequestParam String id) {
        return AppReply.success(standardBasicDataService.deleteByPrimaryKey(id));
    }

    /**
     * saveBasicFieldOptionInfo 保存字段的选择项
     *
     * @param item:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 15:07
     * @change 2021/3/26 15:07 by q8541q@163.com for init
     * @since 1.0.0
     */
    @PostMapping("/saveBasicFieldOptionInfo")
    @AppAuthAnnotation
    public AppReply saveBasicFieldOptionInfo(@RequestBody StandardBasicDataOptionVo item)
            throws McpException {
        return AppReply.success(standardBasicDataOptionService.save(item));
    }

    /**
     * deleteBasicFieldOptionInfo 删除字段的选择项
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 15:44
     * @change 2021/3/26 15:44 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/deleteBasicFieldOptionInfo")
    @AppAuthAnnotation
    public AppReply deleteBasicFieldOptionInfo(@RequestParam String id) {
        return AppReply.success(standardBasicDataOptionService.deleteByPrimaryKey(id));
    }

    /**
     * getBasicFieldOptionInfoById 根据元数据ID 获取 选择项
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/4/13 16:01
     * @change 2021/4/13 16:01 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getBasicFieldOptionInfoById")
    public AppReply getBasicFieldOptionInfoById(@RequestParam String id) {
        return AppReply.success(standardBasicDataOptionService.getBasicFieldOptionInfoById(id));
    }

    /**
     * saveStandDetailInfo 记录 或更新 标准详细信息
     *
     * @param item:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 15:46
     * @change 2021/3/26 15:46 by q8541q@163.com for init
     * @since 1.0.0
     */
    @PostMapping("/saveStandDetailInfo")
    @AppAuthAnnotation
    public AppReply saveStandDetailInfo(@RequestBody SaveStandardDetailVo item) {
        return AppReply.success(standardDetailService.save(item));
    }

    /**
     * deleteStandDetailInfo 删除 标准记录详情
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 16:48
     * @change 2021/3/26 16:48 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/deleteStandDetailInfo")
    @AppAuthAnnotation
    public AppReply deleteStandDetailInfo(@RequestParam String id) {
        return AppReply.success(standardDetailService.deleteByPrimaryKey(id));
    }

    /**
     * getStandDetailInfo 详细标准的详情-
     *
     * @param id: 标准记录ID
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 16:58
     * @change 2021/3/26 16:58 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getStandDetailInfo")
    public AppReply getStandDetailInfo(@RequestParam String id) throws BindException {
        SaveStandardDetailVo saveStandardDetailVo = standardDetailService.getStandDetailInfo(id);
        return AppReply.success(saveStandardDetailVo);
    }

    /**
     * getStandDetailInfoList 详细标准的列表
     *
     * @param id:
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/3/26 17:05
     * @change 2021/3/26 17:05 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getStandDetailInfoList")
    public AppReply getStandDetailInfoList(@RequestParam String id) throws BindException {
        List<SaveStandardDetailVo> list = standardDetailService.getStandDetailInfoList(id);
        return AppReply.success(list);
    }

    @GetMapping("/downloadStandModel")
    //    @AppAuthAnnotation
    public void downloadStandModel(@RequestParam String id) throws IOException {
        standardService.downloadStandModel(id);
    }

    @GetMapping("/downloadStandModelAll")
//    @AppAuthAnnotation
    public void downloadStandModelAll(HttpServletResponse response) {
        standardService.downloadStandModelAll(response);
    }


    @PostMapping("/importExcelAll")
    public AppReply importExcelAll(MultipartFile file) {
        return standardService.importExcelAll(file);
    }

    @GetMapping("/downloadSql")
    public void downloadSql(HttpServletResponse response) {
        standardService.downloadSql(response);
    }

    @PostMapping("/importSql")
    public AppReply importSql(MultipartFile file) {
        try {
            standardService.importSql(file);
            return AppReply.success("导入成功");
        } catch (IOException e) {
            e.printStackTrace();
            return AppReply.error("导入失败");
        }
    }

    //    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public AppReply importExcel(MultipartFile file, String id, String basicId) {
//        long t1 = System.currentTimeMillis();
//        List<List<String>> lists = ExcelUtils.readExcel(file);
//        long t2 = System.currentTimeMillis();
//        String msg = standardService.importBasicInfoByExcel(lists, id, basicId);
//        log.info(String.format("read over! cost:%sms", (t2 - t1)));
////        return standardService.batchImportStand(list);
//        return AppReply.success(msg);
//    }
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public AppReply importExcel(MultipartFile file, String id, String basicId) {
        long t1 = System.currentTimeMillis();
        List<List<String>> lists = ExcelUtils.readExcel(file);
        long t2 = System.currentTimeMillis();
        String msg = standardService.importBasicInfoByExcel(lists, id, basicId);
        log.info(String.format("read over! cost:%sms", (t2 - t1)));
        //        return standardService.batchImportStand(list);
        return AppReply.success(msg);
    }

    @GetMapping("/getAssociationInfo/{choosedSjbs}")
    public AppReply getAssociationInfo(@PathVariable String choosedSjbs) throws IOException {
        PageData pageData = getPageData();
        //pageData.put("sjbs", choosedSjbs);
        PageResult pageResult = standardService.getAssociationInfo2(pageData);
        return AppReply.success(pageResult);
    }

    @GetMapping("/getAssociationInfo2")
    public AppReply getAssociationInfo2(@RequestParam String query) throws IOException {
        List<StandardVo> list = standardService.getAssociationInfo3(query);
        return AppReply.success(list);
    }

    /**
     * 功能描述：统计标准与规范的标准数据数量
     *
     * @return 标准类型 - type；数据数量 - num
     */
    @GetMapping("/picChart")
    public ResponseEntity<List<Map>> picChart() {
        return ResponseEntity.ok(standardService.picChart());
    }

    /**
     * 功能描述：统计标准与规范的标准数据访问量
     *
     * @return 标准类型 - type；数据访问量 - num
     */
    @GetMapping("/cylindricalChart")
    public ResponseEntity<List<Map>> cylindricalChart() {
        return ResponseEntity.ok(standardService.cylindricalChart());
    }
}
